//
//  WH_ListTableViewCell.m
//  App_6.23
//
//  Created by 芦珊 on 16/6/28.
//  Copyright © 2016年 lushan. All rights reserved.
//

#import "WH_ListTableViewCell.h"
#import "WH_Home.h"
@interface WH_ListTableViewCell ()

@property (nonatomic ,strong)UIImageView *headImgView;
@property (nonatomic ,strong)UILabel *titleLabel;
@property (nonatomic ,strong)UILabel *detailLabel;
@property (nonatomic ,strong)UILabel *countLabel;

@property (nonatomic ,strong)UIImageView *minusImgView;
@property (nonatomic ,strong)UIImageView *plusImgView;
@property (nonatomic ,strong)UILabel *numberLabel;

@property (nonatomic ,assign)NSInteger number;

@property (nonatomic ,strong)UIButton *chooseButton;

@property (nonatomic, strong) NSMutableArray *periodArr;//选中cell的数组
@end

@implementation WH_ListTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
        self.periodArr = [NSMutableArray array];

    }
    return self;
}

- (void)createSubView{
    
    
    //图片
    self.headImgView = [[UIImageView alloc] initWithFrame:CGRectMake(self.bounds.origin.x + ((SCREEN_HEIGHT / 5) / 3), self.bounds.origin.y, SCREEN_WIDTH / 4, SCREEN_WIDTH / 4)];
    self.headImgView.backgroundColor = [UIColor grayColor];
    self.headImgView.userInteractionEnabled = YES;
    //给图片加轻拍手势
    UITapGestureRecognizer* headTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headTapAction)];
    [_headImgView addGestureRecognizer:headTap];
    [self.contentView addSubview:self.headImgView];
    
    //勾选按钮
    self.chooseButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.chooseButton setBackgroundImage:[UIImage imageNamed:@"圆形未选中.png"] forState:UIControlStateNormal];
    [self.chooseButton addTarget:self action:@selector(chooseAction) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_chooseButton];
    [self.chooseButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.headImgView.mas_left).offset(-5);
        make.top.equalTo(self.contentView.mas_top).offset((SCREEN_HEIGHT / 5) / 3);
        make.height.width.equalTo(@((SCREEN_HEIGHT / 5) / 5));
    }];

    
    
    
    
    //名称
    self.titleLabel = [[UILabel alloc] init];
//    self.titleLabel.backgroundColor = [UIColor greenColor];
    self.titleLabel.text = @"宝贝名 骄傲的金额附件为附近;我覅偶尔玩;覅忘记哦";
    self.titleLabel.numberOfLines = 0;
    self.titleLabel.textColor = [UIColor blackColor];
    self.titleLabel.font = [UIFont systemFontOfSize:12];
    [self.contentView addSubview:_titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headImgView.mas_right).offset(10);
        make.right.equalTo(self.contentView).offset(-10);
        make.top.equalTo(self.contentView);
        make.height.equalTo(@((SCREEN_HEIGHT / 5) / 3));
    }];
    
    //详细信息
    self.detailLabel = [[UILabel alloc] init];
    //self.detailLabel.backgroundColor = ARCDOM_4;
    self.detailLabel.text = @"信息";
    self.detailLabel.textColor = [UIColor lightGrayColor];
    self.detailLabel.font = [UIFont systemFontOfSize:12];
    [self.contentView addSubview:_detailLabel];
    [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headImgView.mas_right).offset(10);
        make.right.equalTo(self.contentView).offset(-10);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(5);
        make.height.equalTo(@((SCREEN_HEIGHT / 5) / 6));
    }];
    
    UIImageView *imageV = [[UIImageView alloc] init];
    imageV.image = [UIImage imageNamed:@"加减.png"];
    [self.contentView addSubview:imageV];
    [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.headImgView.mas_right).offset(10);
        make.top.equalTo(self.detailLabel.mas_bottom).offset(5);
        make.height.equalTo(@((SCREEN_HEIGHT / 5) / 3-10));
        
        make.width.equalTo(@((SCREEN_HEIGHT / 15-10)+(SCREEN_HEIGHT / 10)+(SCREEN_HEIGHT / 15-10)));
    }];
    
    //减号
    self.minusImgView = [[UIImageView alloc] init];
    //self.minusImgView.backgroundColor = ARCDOM_4;
    self.minusImgView.userInteractionEnabled = YES;
    //给减号加轻拍手势
    UITapGestureRecognizer* tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(minusTapAction)];
    [_minusImgView addGestureRecognizer:tap];
    
    [self addSubview:_minusImgView];
    
    [self.minusImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headImgView.mas_right).offset(10);
        make.top.equalTo(self.detailLabel.mas_bottom).offset(5);
        make.height.width.equalTo(@((SCREEN_HEIGHT / 5) / 3-10));
    }];
    
    //数量
    self.numberLabel = [[UILabel alloc] init];
    //self.numberLabel.backgroundColor = ARCDOM_4;
    self.numberLabel.text = @"1";
    self.numberLabel.textColor = [UIColor redColor];
    self.numberLabel.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:self.numberLabel];
    [self addSubview:_numberLabel];
    
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.minusImgView.mas_right);
        make.top.height.equalTo(self.minusImgView);
        make.width.equalTo(@((SCREEN_HEIGHT / 5) / 2));
    }];
    
    //加号
    self.plusImgView = [[UIImageView alloc] init];
    //self.plusImgView.backgroundColor = ARCDOM_4;
    self.plusImgView.userInteractionEnabled = YES;
    //给加号加轻拍手势
    UITapGestureRecognizer* plusTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(plusTapAction)];
    [_plusImgView addGestureRecognizer:plusTap];
    [self addSubview:_plusImgView];
    
    [self.plusImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.numberLabel.mas_right);
        make.height.top.width.equalTo(self.minusImgView);
    }];
    
    //人次
    self.countLabel = [[UILabel alloc] init];
    //self.countLabel.backgroundColor = ARCDOM_4;
    self.countLabel.text = @"人次";
    self.countLabel.textColor = [UIColor lightGrayColor];
    self.countLabel.font = [UIFont systemFontOfSize:12];
    [self.contentView addSubview:_countLabel];
    
    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.plusImgView.mas_right).offset(5);
        make.top.equalTo(self.detailLabel.mas_bottom).offset(5);
        make.height.width.equalTo(self.minusImgView);
    }];
    
    
    
}

- (void)setHomeModel:(WH_Home *)homeModel{
    if (_homeModel != homeModel) {
        _homeModel = homeModel;
    }
    _number = [self.homeModel.product_count integerValue];

    [self.headImgView sd_setImageWithURL:[NSURL URLWithString:homeModel.product_image] placeholderImage:PLACEHOLDER_CELL_SAME];
    self.titleLabel.text = homeModel.product_name;
    NSString *numStr = [NSString stringWithFormat:@"已购%@人次",_homeModel.period_num];
    self.detailLabel.text = [NSString stringWithFormat:@"总需%@人次,   %@",homeModel.product_price,numStr];
    self.numberLabel.text = _homeModel.product_count;
    [self.chooseButton setBackgroundImage:[UIImage imageNamed:@"圆形未选中.png"] forState:UIControlStateNormal];
    if (_homeModel.selected) {
        [self.chooseButton setBackgroundImage:[UIImage imageNamed:@"圆形选中.png"] forState:UIControlStateNormal];
    }
    
}

#pragma mark -减号响应方法
-(void)minusTapAction{
    
    if (_number == 1)
    {
        NSLog(@"no");
    }
    else
    {
        [WSProgressHUD showImage:nil status:@"- " maskType:WSProgressHUDMaskTypeClear];

        _number--;
        
        _numberLabel.text = [NSString stringWithFormat:@"%ld",_number];
        
        NSString *body =[NSString stringWithFormat:@"action=cart_edit&user_id=%@&product_period=%@&product_num=%ld", NSUSERID,self.homeModel.product_period,_number] ;
        [WH_AppTools postUrl:URLstr body:body result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
            NSDictionary *dic = result;
            NSString *status = [dic objectForKey:@"status"];
            NSString *message = [dic objectForKey:@"message"];
            if ([status isEqualToString:@"success"]) {
                
                [self.headdelegate freshView];

            }else{
                
                [WSProgressHUD showImage:nil status:message];
                
            }
            
        } failure:^(NSError *error) {
            
            
        }];

    }
}
#pragma mark -加号响应方法
-(void)plusTapAction{
    

    
    NSInteger a = [_homeModel.product_price integerValue] - [_homeModel.period_num integerValue];
    if (_number >= a) {
        
        NSLog(@"%ld",a);
    }else{
        [WSProgressHUD showImage:nil status:@"+" maskType:WSProgressHUDMaskTypeClear];
        _number++;
        _numberLabel.text = [NSString stringWithFormat:@"%ld",_number];
        NSString *body =[NSString stringWithFormat:@"action=cart_edit&user_id=%@&product_period=%@&product_num=%ld", NSUSERID,self.homeModel.product_period,_number] ;
        [WH_AppTools postUrl:URLstr body:body result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
            NSDictionary *dic = result;
            NSString *status = [dic objectForKey:@"status"];
            NSString *message = [dic objectForKey:@"message"];
            if ([status isEqualToString:@"success"]) {
                
                [self.headdelegate freshView];
            }else{
                
                [WSProgressHUD showImage:nil status:message];
                
            }
            
        } failure:^(NSError *error) {
            
            
        }];

        
    }

    NSLog(@"加号响应方法");
}
#pragma mark -图片响应方法
- (void)headTapAction{
    
    [self.headdelegate headImgPush:_homeModel.product_id];
    
}
#pragma mark -勾选按钮响应方法(要判断解决重用,在set方法里)
- (void)chooseAction{
    
    if (self.homeModel.selected == false) {
        [self.chooseButton setBackgroundImage:[UIImage imageNamed:@"圆形选中.png"] forState:UIControlStateNormal];
         NSLog(@"选中indexPath.row ========== %ld",(long)self.indexRow);
        [self.headdelegate sendModelToC:_homeModel];
        
    }else{
        [self.chooseButton setBackgroundImage:[UIImage imageNamed:@"圆形未选中.png"] forState:UIControlStateNormal];
        NSLog(@"未选中indexPath.row ========== %ld",self.indexRow);
        [self.headdelegate deleModelToC:_homeModel];

    }
    
    self.homeModel.selected = !self.homeModel.selected;
        
}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
